<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2023/7/17 0019
 * Time: 16:45
 */

namespace app\common\service;


use think\Db;

class StoreGoodsFirstService extends Service
{
    protected $tableName = 'sms_mk_goods_firsts';

    public function addUpdateFun($storeId){
        //查找第一个商品
        $goods = Db::table('sms_mk_goods')->alias('g')
            ->join('sms_mk_store_types st','st.id = g.sms_store_types_id')
            ->where('g.sms_stores_id','=',$storeId)
            ->order('st.sort','asc')
            ->order('g.id','desc')
            ->limit(1)
            ->field('g.id')
            ->select();
        if(!@$goods) return;
        $goodsId = $goods[0]['id'];
        //添加或更新
        $storeGoods = Db::table('sms_mk_goods_firsts')->find(['store_id'=>$storeId]);
        if($storeGoods)
            Db::table('sms_mk_goods_firsts')->where('store_id','=',$storeId)->update(['goods_id'=>$goodsId]);
        else{
            Db::table('sms_mk_goods_firsts')->insert(['goods_id'=>$goodsId,'store_id'=>$storeId]);
        }
    }



    public function datalist()
    {
        $lng = @$this->param['lng'];
        $lat = @$this->param['lat'];
        $this->setDb('store_goods_view');
        if($lng && $lat){
            $this->db->field("*,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat * PI()/ 180-lat * PI()/ 180)/ 2),2)+ COS( $lat * PI()/ 180 )* COS( lat * PI()/ 180 )* POW(SIN(($lng * PI()/ 180-lng * PI()/ 180)/ 2),2)))* 1000) AS distance")
                ->order('distance','asc');
        }
        $store_id = @$this->param['store_id'];
        $search_name = @$this->param['search_name'];
        $types_first_id = @$this->param['types_first_id'];
        $types_second_id = @$this->param['types_second_id'];
        $types_third_id = @$this->param['types_third_id'];
        $store_types_id = @$this->param['store_types_id'];

        if($types_first_id)
            $this->db = $this->db->where('types_first_id','=',$types_first_id);
        elseif($types_second_id){
            $this->db = $this->db->where('types_second_id','=',$types_second_id);
        }elseif($types_third_id){
            $this->db = $this->db->where('types_third_id','=',$types_third_id);
        }
        if($store_id)
            $this->db = $this->db->where('store_id','=',$store_id);
        if($store_types_id)
            $this->db = $this->db->where('store_types_id','=',$store_types_id);
        if($search_name){
            $this->db = $this->db->whereOr('types_first_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_types_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('types_second_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('types_third_text','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('name','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_name','like','%'.$search_name.'%');
            $this->db = $this->db->whereOr('store_tag','like','%'.$search_name.'%');
        }

        $result =  parent::datalist(); // TODO: Change the autogenerated stub
        //处理图片 thumb后期会保存数据
        foreach ($result['data'] as $k=>$item){
            if($item['thumb']) $result['data'][$k]['thumb'] = explode(',',$item['thumb']);
        }
        return $result;
    }


    public function getStoreGoods($id){
        $this->setDb('store_goods_view');
        $this->db->where('store_id','=',$id);

        $lng = @$this->param['lng'];
        $lat = @$this->param['lat'];
        if($lng && $lat){
            $this->db->field("*,ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(($lat * PI()/ 180-lat * PI()/ 180)/ 2),2)+ COS( $lat * PI()/ 180 )* COS( lat * PI()/ 180 )* POW(SIN(($lng * PI()/ 180-lng * PI()/ 180)/ 2),2)))* 1000) AS distance");
        }
        return $this->db->select();
    }
}